package com.nd.android.u.ui;

import ims.IMSdk;
import ims.IMSdkEntry;
import ims.cmd.GroupCmd;
import ims.cmd.PersonCmd;
import ims.outInterface.MessageParserFactory;
import android.content.Context;

import com.common.StarAppConfig;
import com.common.android.utils.smiley.Smileyhelper;
import com.nd.android.u.business.db.UDatabase;
import com.nd.android.u.chatInterfaceImpl.ChatInterfaceImpl;
import com.nd.android.u.commonWidget.NotificationMsg;
import com.nd.android.u.controller.ChatConst.LocalMessageType;
import com.nd.android.u.controller.ChatConfiguration;
import com.nd.android.u.controller.ChatGlobalVariable;
import com.nd.android.u.controller.MessageConst.GroupType;
import com.nd.android.u.controller.ResMapper;
import com.nd.android.u.controller.factory.ChatBottomFunctionFactory;
import com.nd.android.u.controller.factory.ChatDaoFactory;
import com.nd.android.u.controller.factory.ContactFactory;
import com.nd.android.u.controller.factory.ContactLongClickMenuFactory;
import com.nd.android.u.controller.factory.DataSupplierFactory;
import com.nd.android.u.controller.factory.GroupFactory;
import com.nd.android.u.controller.factory.MessageFactory;
import com.nd.android.u.controller.factory.MessageLongClickMenuFactory;
import com.nd.android.u.controller.factory.ShareFileFactory;
import com.nd.android.u.controller.groupSupplier.DefaultGroupSupplier;
import com.nd.android.u.controller.innerInterface.IShareFile;
import com.nd.android.u.imSdk.CommonNotifyImpl;
import com.nd.android.u.imSdk.GroupFeedbackReceiver;
import com.nd.android.u.imSdk.GroupLoginManager;
import com.nd.android.u.imSdk.GroupMessageReceiver;
import com.nd.android.u.imSdk.GroupSystemMsgReceiver;
import com.nd.android.u.imSdk.PersonMessageReceiver;
import com.nd.android.u.imSdk.SdkParaSupplier;
import com.nd.android.u.ui.StarAppChatConst.MessageType;
import com.nd.android.u.ui.chatBottomFunction.BottomFunction_Camera;
import com.nd.android.u.ui.chatBottomFunction.BottomFunction_Photo;
import com.nd.android.u.ui.chatInterfaceImpl.IChatCallOtherModel_UI;
import com.nd.android.u.ui.chatUiUtils.StringMessageAnalyser;
import com.nd.android.u.ui.contactCreator.AppContactCreator;
import com.nd.android.u.ui.contactCreator.AssistantCreator;
import com.nd.android.u.ui.contactCreator.GroupContactCreator;
import com.nd.android.u.ui.contactCreator.PersonContactCreator;
import com.nd.android.u.ui.contactCreator.SystemContactCreator;
import com.nd.android.u.ui.creator.AssistantDataSupplierCreator;
import com.nd.android.u.ui.creator.StarAppDownloadCreator;
import com.nd.android.u.ui.creator.StarAppUploadCreator;
import com.nd.android.u.ui.longClickMenu.contact.ContactLongClickMenu_Del;
import com.nd.android.u.ui.longClickMenu.messageList.MenuItemCopy;
import com.nd.android.u.ui.longClickMenu.messageList.MenuItemDelete;
import com.nd.android.u.ui.longClickMenu.messageList.MenuItemForward;
import com.nd.android.u.ui.longClickMenu.messageList.MenuItemResend;
import com.nd.android.u.ui.longClickMenu.messageList.MenuItemShare;
import com.nd.android.u.ui.messageCreator.GroupMessageCreator;
import com.nd.android.u.ui.messageCreator.PersonMessageCreator;
import com.nd.android.u.ui.messageCreator.SystemMessageCreator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.product.android.business.login.LoginFlag;

/**
 * 聊天模块入口，聊天模块需要用到的一些配置参数、差异化的回调都在这里初始化
 * <br>Created 2014-7-11 下午5:13:30
 * @version  
 * @author   cb		
 * @see 	 
 */
public enum ChatEntry {
	INSTANCE;
	public DisplayImageOptions chatOptions = null;
	public IChatCallOtherModel_UI chatCallOtherModel_UI;
	/**父联系人获取接口*/
	private ChatEntry(){
		IMSdkEntry.INSTANCE.sdkDataSupplier=new SdkParaSupplier();
		IMSdkEntry.INSTANCE.commonNotify=new CommonNotifyImpl();
		IMSdkEntry.INSTANCE.personMsgNotify=new PersonMessageReceiver();
		IMSdkEntry.INSTANCE.groupFeedbackNotify=new GroupFeedbackReceiver();
		IMSdkEntry.INSTANCE.groupMsgNotify=new GroupMessageReceiver();
		IMSdkEntry.INSTANCE.groupSystemMsgNotify=new GroupSystemMsgReceiver();
		ChatConfiguration.INSTANCE.isReceiveSystemMessage=true;
	}
	/**
	 * 初始化聊天模块相关的配置与接口
	 * <br>Created 2014-8-1 下午4:59:53
	 * @author  cb
	*/
	public void init(Context context){
		IMSdkEntry.INSTANCE.init(context);
		initResMapper();
		MessageParserFactory.INSTANCE.registDefault();
		registMessageCreator();
		registContactCreator();
		registChatBottomFunctionFactory();
		registLongclickMenu();
		ShareFileFactory.INSTANCE.registShareFileInterface(IShareFile.UPLOAD, new StarAppUploadCreator());
		ShareFileFactory.INSTANCE.registShareFileInterface(IShareFile.UPLOAD_GROUP, new StarAppUploadCreator());
		ShareFileFactory.INSTANCE.registShareFileInterface(IShareFile.DOWNLOAD, new StarAppDownloadCreator());
		if(StarAppConfig.getBooleanConfig(StarAppConfig.IS_OPEN_GROUP)){//由开关控制是否登陆群以接收消息
			GroupFactory.INSTANCE.registGroupsupplier(GroupType.NORMAL, new DefaultGroupSupplier());
		}
		DataSupplierFactory.INSTANCE.registDataSupplier(MessageType.ASSISTANT, new AssistantDataSupplierCreator());
		//以下代码用于异常消息反馈
		ChatGlobalVariable.getInstance().messageTypeMap.put(PersonCmd.class, LocalMessageType.PERSON);
		ChatGlobalVariable.getInstance().messageTypeMap.put(GroupCmd.class, LocalMessageType.GROUP);
		ChatInterfaceImpl.INSTANCE.messageAnalyser=new StringMessageAnalyser();
	}
	
	/**
	 * 注册消息生产者
	 * <br>Created 2014-9-16 下午5:31:32
	 * @author  cb
	*/
	private void registMessageCreator(){
		MessageFactory.INSTANCE.registMessageCreator(LocalMessageType.PERSON, new PersonMessageCreator());
		MessageFactory.INSTANCE.registMessageCreator(LocalMessageType.SYSTEM, new SystemMessageCreator());
		MessageFactory.INSTANCE.registMessageCreator(LocalMessageType.GROUP, new GroupMessageCreator());
	}
	
	/**
	 * 注册联系人生产者
	 * <br>Created 2014-10-30 下午2:42:21
	 * @author  cb
	*/
	private void registContactCreator(){
		ContactFactory.INSTANCE.registContactCreator(LocalMessageType.PERSON, new PersonContactCreator());		
		ContactFactory.INSTANCE.registContactCreator(LocalMessageType.GROUP, new GroupContactCreator());		
		ContactFactory.INSTANCE.registContactCreator(LocalMessageType.APP, new AppContactCreator());		
		ContactFactory.INSTANCE.registContactCreator(LocalMessageType.SYSTEM, new SystemContactCreator());		
		ContactFactory.INSTANCE.registContactCreator(MessageType.ASSISTANT, new AssistantCreator());		
	}
	
	/**
	 * 注册长按事件
	 * <br>Created 2014-9-19 下午4:31:35
	 * @author  cb
	*/
	private void registLongclickMenu(){
		ContactLongClickMenuFactory.INSTANCE.addMessageMenuItem(new ContactLongClickMenu_Del());
		MessageLongClickMenuFactory.getInstance().addMenuItem(new MenuItemCopy());
		MessageLongClickMenuFactory.getInstance().addMenuItem(new MenuItemDelete());
		MessageLongClickMenuFactory.getInstance().addMenuItem(new MenuItemShare());
		MessageLongClickMenuFactory.getInstance().addMenuItem(new MenuItemResend());
		MessageLongClickMenuFactory.getInstance().addMenuItem(new MenuItemForward());
	}
	
	/**
	 * 注册聊天底部菜单
	 * <br>Created 2014-9-19 下午4:36:09
	 * @author  cb
	*/
	private void registChatBottomFunctionFactory(){
//		ChatBottomFunctionFactory.getInstance().addListGridItem(new BottomFunction_Smiley());
		ChatBottomFunctionFactory.getInstance().addListGridItem(new BottomFunction_Camera());
		ChatBottomFunctionFactory.getInstance().addListGridItem(new BottomFunction_Photo());
	}
	
	/**
	 * 切换帐号/切换身份时清除聊天模块相关痕迹
	 * <br>Created 2014-8-5 下午2:36:54
	 * @author  cb
	*/
	public void clearChatRelative(){
		IMSdk.logoutIMS();                                //IMS停止收发消息
		NotificationMsg.getInstance().cancelNotify();	      //取消通知栏
		//清除上传语音/文件的SID，否则可能引起新登录用户的SID错乱
		ChatGlobalVariable.getInstance().mShareFileServiceSid="";
		ChatGlobalVariable.getInstance().getFackBackList().clear();  //反馈列表
		GroupLoginManager.INSTANCE.clear(); //清空登录群接收群列表

	}
	
	/**
	 * 关闭聊天模块数据库
	 * <br>Created 2014-8-5 下午2:40:35
	 * @author  cb
	*/
	public void closeChatDb(){
		ChatDaoFactory.getInstance().clear(); //关闭聊天工程数据库
		UDatabase db = UDatabase.getInstance();
		if (db != null) {
			db.close();	// 关闭聊天数据库
		}
	}
	
	/**
	 * 登出IMS
	 * <br>Created 2014-8-5 下午2:55:16
	 * @author  cb
	*/
	public void logoutIMS(){
		IMSdk.logoutIMS();
	}
	
	/**
	 * 登录IMS
	 * <br>Created 2014-8-5 下午3:06:58
	 * @author  cb
	*/
	public void loginIMS(){
		IMSdk.loginIMS();
	}

	/**
	 * 完全退出系统
	 * 
	 * <br>
	 * Created 2014-11-25 下午4:08:38
	 * 
	 * @param ctx
	 *            上下文
	 * @param doExit
	 *            如果在登录界面退出，就不要执行登录标志位的设置
	 * @author : HuangYK
	 */
	public void exitSystemIMS(Context ctx, boolean doExit) {
		clearChatRelative();
		closeChatDb();
		if (doExit) {
            Smileyhelper.getInstance().clearInstance();
			LoginFlag.saveEXITEnumType();
		}
		IMSdk.stopService(ctx);
	}
	
	/**
	 * 群资料下载成功，登录群
	 * <br>Created 2014-8-5 下午3:06:58
	 * @author  zhy
	*/
	public void loginAllGroup(){
		GroupLoginManager.INSTANCE.loginAllGroup(false);
	}
	
	/**
	 * 为业务层设置资源
	 * <br>Created 2014-10-30 下午2:33:14
	 * @author  cb
	*/
	public void initResMapper(){
		/**通知栏显示的图标*/
		ResMapper.INSTANCE.NOTI_ICON=R.drawable.uu_6;
		/**通知时的铃声*/
		ResMapper.INSTANCE.NOTI_BREEDING=R.raw.breeding;
		/**还有多少条未读消息notify_remain*/
		ResMapper.INSTANCE.NOTI_REMAIN_MSG=R.string.notify_remain;
		/**语音消息*/
		ResMapper.INSTANCE.MSG_AUDIO=R.string.chat_audio;
		/**图片消息*/
		ResMapper.INSTANCE.MSG_IMG=R.string.image_info;
		/**视频消息*/
		ResMapper.INSTANCE.MSG_VIDEO=R.string.chat_vidio;
		/**文件消息*/
		ResMapper.INSTANCE.MSG_FILE=R.string.chat_file;
		/**应用消息*/
		ResMapper.INSTANCE.MSG_APP=R.string.app_msg;
		/**系统公告*/
		ResMapper.INSTANCE.MSG_BROADCAST=R.string.broadcase;
		/**系统消息*/
		ResMapper.INSTANCE.MSG_SYSTEM=R.string.system_msg;
		/**应用名称（是指本应用的名称，比如网龙99游）*/
		ResMapper.INSTANCE.APP_NAME=R.string.app_name;
		/**正在重发chat_resending*/
		ResMapper.INSTANCE.OP_RESEND=R.string.chat_resending;
		/**取消*/
		ResMapper.INSTANCE.OP_CANCEL=R.string.cancel;
		/**pc端已接收了文件clientservicefile*/
		ResMapper.INSTANCE.OP_PC_RECEIVED_FILE=R.string.clientservicefile;
		/**群解散group_dissolved*/
		ResMapper.INSTANCE.GROUP_DISMISS=R.string.group_dissolved;
		/**被踢出manager_move_u_out*/
		ResMapper.INSTANCE.GROUP_KICKED=R.string.manager_move_u_out;
		/**成员离开群member_quit*/
		ResMapper.INSTANCE.GROUP_MEMBER_QUIT=R.string.member_quit;
		/**自己离开群self_quit*/
		ResMapper.INSTANCE.GROUP_SELF_QUIT=R.string.self_quit;
		
	}
}
